package com.sikao.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.CategoryAttrNewBean;
import com.sikao.app.bean.MallGoodsDetailBean;
import com.sikao.app.ui.AddGoodsStepTwoActivity;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.CustomExpandableListView;
import com.sikao.app.view.MyListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 发布商品第二步 类型是4 多选 有浮动值 下单的时候用到的
 */
public class MlvMallOrderExtrasItemForTypeFourAdapter extends BaseExpandableListAdapter {
    private List<MallGoodsDetailBean.ResBean.GoodsAttrsBean> objects  ;
    MlvMallOrderExtrasItemForTypeFourAdapterOnClickListener   mlvMallOrderExtrasItemForTypeFourAdapterOnClickListener;
    private Context context;
    private LayoutInflater layoutInflater;

    public MlvMallOrderExtrasItemForTypeFourAdapter(Context context, List<MallGoodsDetailBean.ResBean.GoodsAttrsBean> objects,MlvMallOrderExtrasItemForTypeFourAdapterOnClickListener mlvMallOrderExtrasItemForTypeFourAdapterOnClickListener) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects = objects;
        this.mlvMallOrderExtrasItemForTypeFourAdapterOnClickListener =mlvMallOrderExtrasItemForTypeFourAdapterOnClickListener;
    }

    @Override
    public int getGroupCount() {
        return objects.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return objects.get(i).getDefault_value().size();
    }

    @Override
    public MallGoodsDetailBean.ResBean.GoodsAttrsBean getGroup(int i) {
        return objects.get(i);
    }

    @Override
    public String getChild(int i, int i1) {
        return objects.get(i).getDefault_value().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int parentPos, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.mll_add_step_prices_type_three_new_expan_item, null);
        }

        if (((CustomExpandableListView) viewGroup).isOnMeasure) {
            //如果是onMeasure调用的就立即返回
            return view;
        }

        Utils.showLog("=======getGroupView  parentPos:" + parentPos);
        view.setTag(R.layout.mll_add_step_prices_type_three_new_expan_item, parentPos);
        view.setTag(R.layout.mll_add_step_prices_type_three_child_fororder_item, -1);
        TextView text = (TextView) view.findViewById(R.id.tv_name);
        text.setText(getGroup(parentPos).getName());
        return view;
    }

    @Override
    public View getChildView(final int parentPos, final int childPos, boolean b, View view, final ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {

            LayoutInflater inflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.mll_add_step_prices_type_three_child_fororder_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (((CustomExpandableListView) viewGroup).isOnMeasure) {
            //如果是onMeasure调用的就立即返回
            return view;
        }
        viewHolder.tvThreeChildValue.setText(getChild(parentPos, childPos));
        viewHolder.etThreeChildChangePrice.setText(objects.get(parentPos).getChange_price().get(childPos));

        if ("true".equals(objects.get(parentPos).getIs_default().get(childPos))) {

            viewHolder.cbThreeChildIsDefault.setChecked(true);
        }else{

            viewHolder.cbThreeChildIsDefault.setChecked(false);
        }

        viewHolder.cbThreeChildIsDefault.setTag(R.id.parentPos,parentPos);
        viewHolder.cbThreeChildIsDefault.setTag(R.id.childPos,childPos);
        viewHolder.cbThreeChildIsDefault.setTag(R.id.ischecked,viewHolder.cbThreeChildIsDefault.isChecked());
        viewHolder.cbThreeChildIsDefault.setOnClickListener(mlvMallOrderExtrasItemForTypeFourAdapterOnClickListener);


        return view;
    }

    protected class ViewHolder {
        private CheckBox cbThreeChildIsUse;
        private CheckBox cbThreeChildIsDefault;
        private TextView tvThreeChildValue;
        private TextView etThreeChildChangePrice;

        public ViewHolder(View view) {
            cbThreeChildIsDefault = (CheckBox) view.findViewById(R.id.cb_three_child_isdefault);
            cbThreeChildIsUse = (CheckBox) view.findViewById(R.id.cb_three_child_isuse);

            tvThreeChildValue = (TextView) view.findViewById(R.id.tv_three_child_value);
            etThreeChildChangePrice = (TextView) view.findViewById(R.id.et_three_child_change_price);
        }
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    //点击回调
    public static abstract class MlvMallOrderExtrasItemForTypeFourAdapterOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            v.setTag(R.id.ischecked,((CheckBox) v ).isChecked());
            myOnClick( v);
        }

        public abstract void myOnClick(View v);
    }
}
